<template>
  <section class="left-body">
    <div class="content-bty">
      <div class="item-box">
        <div class="item-box-pd">
          <img class="item-box-name" src="/src/assets/imgs/securityManage/one.png">
<!--          <video autoplay webkit-playsinline="true" playsinline controlsList="nodownload" object-fit="fill" poster="" speed="false" class="video-sty">
            <source src="https://tytest.gzvst.top/fanzhajlhsev/profile/upload/2023/03/10/1_20230310181632A003.mp4">
          </video>-->
          <video-play ref="videoMerge" :srcUrl="srcUrl" @playComplete="completePlay" class="video-sty"></video-play>
          <div class="people-content">
            <img src="/src/assets/imgs/securityManage/people.jpg">
            <div class="people-info">
              <a>姓名: 吴国强</a>
              <a>工作单位: 宁波某某有限公司</a>
              <a>联系方式: 13876492648</a>
              <a>抓拍时间: 2024-04-15(周三) 17:33</a>
              <a>抓拍原因: 未带安全帽</a>
            </div>
            <div class="people-warn">
              <img src="/src/assets/imgs/warning.png">
              <span>二级</span>
            </div>
          </div>
        </div>
      </div>
      <div class="item-box">
        <div class="item-box-pd">
          <img class="item-box-name" src="/src/assets/imgs/securityManage/two.png">
          <div class="chart-sty">
            <div id="myChart" class="history"></div>
            <div class="chart-sty-item">
              <div>
                <img src="/src/assets/imgs/warning2.png">
                <span>未带安全帽：8(次)</span>
              </div>
              <div>
                <img src="/src/assets/imgs/warning1.png">
                <span>未穿反光衣：2(次)</span>
              </div>
            </div>
          </div>
          <div class="history-table">
            <div class="history-table-name">
              <span>事件类型</span>
              <span>设备名称</span>
              <span>抓拍时间</span>
            </div>
            <ul class="history-table-list">
              <li>
                <a title="未穿反光衣">未穿反光衣</a>
                <a title="设备A018">设备A018</a>
                <a>2024-07-03 13:32</a>
              </li>
              <li>
                <a>未穿反光衣</a>
                <a>设备A018</a>
                <a>2024-07-03 13:32</a>
              </li>
              <li>
                <a>未穿反光衣</a>
                <a>设备A018</a>
                <a>2024-07-03 13:32</a>
              </li>
              <li>
                <a>未穿反光衣</a>
                <a>设备A018</a>
                <a>2024-07-03 13:32</a>
              </li>
              <li>
                <a>未穿反光衣</a>
                <a>设备A018</a>
                <a>2024-07-03 13:32</a>
              </li>
            </ul>
          </div>
        </div>
      </div>
      <div class="item-box">
        <div class="item-box-pd">
          <img class="item-box-name" src="/src/assets/imgs/securityManage/three.png">
          <div class="anylsis">
            <div id="analyzeChart" class="anylsis-cahrt"></div>
          </div>
        </div>
      </div>
    </div>
  </section>
</template>

<script setup name="SecurityManage">
import {reactive, onMounted, ref, getCurrentInstance, nextTick, computed, provide, onBeforeUnmount} from "vue";
import VideoPlay from "@/components/video/index.vue"

const { proxy } = getCurrentInstance()
const videoMerge = ref(null)
const datalist = [
  {
    name: '未穿反光衣',
    value: 8,
    itemStyle: {
      // opacity: 1,
      color: 'rgba(0,168,255,0.93)',
    }
  }, {
    name: '未带安全帽',
    value: 2,
    itemStyle: {
      // opacity: 1,
      color: '#0cebfd'
    }
  }]
// const srcUrl = ref("http://tlc.gzvst.top/hls/dahua4/index.m3u8")
// const srcUrl = ref("http://192.168.80.6:8080/rtp/34020000001110000001_34020000001320000005.live.mp4")
const srcUrl = ref("http://115.238.130.30:20003/rtp/33021100492000000008_33021101581314000005.live.mp4")
// const srcUrl = ref("http://192.168.80.211:8080/rtp/061467B3/hls.m3u8")

onMounted(() => {
  console.log("安全管理")
  historyChart()

  monthChart()
})

const completePlay = () => {
  let videoElement = videoMerge.value.$refs.videoPlayer;
  // videoElement.play()
}

const monthChart = () => {
  const echarts = proxy.$ECharts
  const echarts1 = echarts.init(document.getElementById("analyzeChart"))

  const option = {
    tooltip: {
      show: true,
      trigger: 'axis',
      // axisPointer: {
      //   type: 'cross',
      //   label: {
      //     backgroundColor: '#6a7985'
      //   }
      // },
      formatter:e=>{
        console.log("e", e)
        return `
            <div style="font-family: fangzheng">
                <div style="color: #333333;font-size: 16px;">${e[0].name}抓拍详情</div>
                <div style="margin: 10px;">
                    <img src="/src/assets/imgs/marker1.png" style="width: 12px;height: 8px;">
                    <span style="color: #333333;">未带安全帽:</span>
                    <a style="color: #666666;">${e[0].data.value}</a><br/>
                </div>
                <div>
                    <img src="/src/assets/imgs/marker2.png" style="width: 10px;height: 12px;">
                    <span style="color: #333333;">未穿反光衣:</span>
                    <a style="color: #666666;">${e[0].data.value1}</a>
                </div>
            </div>
	          `
      }
    },
    grid: {
      top: '14%',
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    backgroundColor: 'rgba(213,208,208,0.3)',
    xAxis: [
      {
        type: 'category',
        nameLocation: 'middle',
        boundaryGap: false,
        data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月'],
        axisLine: {
          show: true,
          lineStyle: {
            color: '#3549AA'
          }
        },
        axisLabel: {
          color: '#3549AA'
        },
        splitLine: {
          show: false
        }
      }
    ],
    yAxis: [
      {
        name: '(件)',
        type: 'value',
        axisLine: {
          show: true,
          lineStyle: {
            color: '#3549AA'
          }
        },
        axisLabel: {
          color: '#3549AA',
          // formatter: '{value}件'
        },
        splitLine: {
          show: false
        }
      }
    ],
    series: [
      {
        name: '未带安全帽',
        type: 'line',
        // stack: 'Total',
        smooth: true,
        lineStyle: {
          width: 3,
          color: '#3549AA'
        },
        showSymbol: false,
/*        areaStyle: {
          opacity: 0.8,
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: 'rgb(0, 221, 255)'
            },
            {
              offset: 1,
              color: 'rgb(77, 119, 255)'
            }
          ])
        },*/
        emphasis: {
          focus: 'series'
        },
        data: [
          {
            value1: 120,
            value: 330
          },
          {
            value1: 282,
            value: 402
          },
          {
            value1: 111,
            value: 181
          },
          {
            value1: 178,
            value: 503
          },
          {
            value1: 220,
            value: 404
          },
          {
            value1: 340,
            value: 449
          },
          {
            value1: 310,
            value2: 269,
            value: 579
          }
        ]
      },
    ]
  };
  echarts1.setOption(option)
  // proxy.$hooks.useRemoveChart(echarts1)
  onBeforeUnmount(() => {
    echarts1.dispose(); // 在组件销毁前手动销毁图表实例
  });
}

const historyChart = () => {
  const echarts = proxy.$ECharts
  const echarts1 = echarts.init(document.getElementById("myChart"))

  const option = getPie3D(datalist, 0);
  echarts1.setOption(option)
  // proxy.$hooks.useRemoveChart(echarts1)
  onBeforeUnmount(() => {
    echarts1.dispose(); // 在组件销毁前手动销毁图表实例
  });
}

// 生成扇形的曲面参数方程，用于 series-surface.parametricEquation
function getParametricEquation(startRatio, endRatio, isSelected, isHovered, k, h, i) {
  // 计算
  let midRatio = (startRatio + endRatio) / 2;

  let startRadian = startRatio * Math.PI * 2;
  let endRadian = endRatio * Math.PI * 2;
  let midRadian = midRatio * Math.PI * 2;

  // 如果只有一个扇形，则不实现选中效果。
  if (startRatio === 0 && endRatio === 1) {
    isSelected = false;
  }

  // 通过扇形内径/外径的值，换算出辅助参数 k（默认值 1/3）
  k = typeof k !== "undefined" ? k : 1 / 3;

  // 计算选中效果分别在 x 轴、y 轴方向上的位移（未选中，则位移均为 0）
  let offsetX = isSelected ? Math.cos(midRadian) * 0.3 : 0;
  let offsetY = isSelected ? Math.sin(midRadian) * 0.1 : 0;
  let offsetZ = i == 1 ? 2 : 0;
  // 计算高亮效果的放大比例（未高亮，则比例为 1）
  let hoverRate = isHovered ? 1.05 : 1.4;

  // 返回曲面参数方程
  return {
    u: {
      min: -Math.PI,
      max: Math.PI * 3,
      step: Math.PI / 32
    },

    v: {
      min: 0,
      max: Math.PI * 2,
      step: Math.PI / 20
    },

    x: function (u, v) {
      if (u < startRadian) {
        return (
            offsetX +
            Math.cos(startRadian) * (1 + Math.cos(v) * k) * hoverRate
        );
      }
      if (u > endRadian) {
        return (
            offsetX + Math.cos(endRadian) * (1 + Math.cos(v) * k) * hoverRate
        );
      }
      return offsetX + Math.cos(u) * (1 + Math.cos(v) * k) * hoverRate;
    },

    y: function (u, v) {
      if (u < startRadian) {
        return (
            offsetY +
            Math.sin(startRadian) * (1 + Math.cos(v) * k) * hoverRate
        );
      }
      if (u > endRadian) {
        return (
            offsetY + Math.sin(endRadian) * (1 + Math.cos(v) * k) * hoverRate
        );
      }
      return offsetY + Math.sin(u) * (1 + Math.cos(v) * k) * hoverRate;
    },

    z: function (u, v) {
      if (u < -Math.PI * 0.5) {
        return offsetZ + Math.sin(u);
      }
      if (u > Math.PI * 2.5) {
        return offsetZ + Math.sin(u);
      }
      // 调整扇形高度
      return offsetZ + (Math.sin(v) > 0 ? 0.1 * h : -1);
    }
  };
}

// 生成模拟 3D 饼图的配置项
function getPie3D(pieData, internalDiameterRatio) {
  let series = [];
  let sumValue = 0;
  let startValue = 0;
  let endValue = 0;
  let k =
      typeof internalDiameterRatio !== "undefined" ?
          (1 - internalDiameterRatio) / (1 + internalDiameterRatio) :
          1 / 3;

  // 为每一个饼图数据，生成一个 series-surface 配置
  for (let i = 0; i < pieData.length; i++) {
    sumValue += pieData[i].value;

    let seriesItem = {
      name: typeof pieData[i].name === "undefined" ?
          `series${i}` : pieData[i].name,
      type: "surface",
      parametric: true,
      wireframe: {
        show: false
      },
      pieData: pieData[i],
      pieStatus: {
        selected: false,
        hovered: false,
        k: k
      }
    };

    if (typeof pieData[i].itemStyle != "undefined") {
      let itemStyle = {};

      typeof pieData[i].itemStyle.color != "undefined" ?
          (itemStyle.color = pieData[i].itemStyle.color) :
          null;
      typeof pieData[i].itemStyle.opacity != "undefined" ?
          (itemStyle.opacity = pieData[i].itemStyle.opacity) :
          null;

      seriesItem.itemStyle = itemStyle;
    }
    series.push(seriesItem);
  }

  // 使用上一次遍历时，计算出的数据和 sumValue，调用 getParametricEquation 函数，
  // 向每个 series-surface 传入不同的参数方程 series-surface.parametricEquation，也就是实现每一个扇形。
  for (let i = 0; i < series.length; i++) {
    endValue = startValue + series[i].pieData.value;

    series[i].pieData.startRatio = startValue / sumValue;
    series[i].pieData.endRatio = endValue / sumValue;
    series[i].parametricEquation = getParametricEquation(
        series[i].pieData.startRatio,
        series[i].pieData.endRatio,
        true,
        false,
        k,
        // 调整扇形高度
        i === 0 ? 10 : 10,
        i
    );

    startValue = endValue;

  }

  series.push({
    name: 'pie2d',
    type: 'pie',
    label: {
      show: false,
      normal: {
        position: 'inner',
/*        formatter: params => {
          return (
              '{percent|' + params.percent.toFixed(0) + '%}'
          );
        },*/
        color:'#000',
        rich: {
          name: {
            fontSize: 34,
          },
          value: {
            fontSize: 20,
            color: '#f10909',
          },
          icon: {
            fontSize: 10,
          },
        },
      }
    },
    labelLine: {
      show: false,
    },
    startAngle: 10, //起始角度，支持范围[0, 360]。
    clockwise: false, //饼图的扇区是否是顺时针排布。上述这两项配置主要是为了对齐3d的样式
    radius: ['20%', '70%'],
    center: ['50%', '50%'],
    data: datalist,
    itemStyle: {
      opacity: 0,
    },
  });

  // 准备待返回的配置项，把准备好的 legendData、series 传入。
  const obj = {
    //animation: false,
    xAxis3D: {
      min: -1,
      max: 1
    },
    yAxis3D: {
      min: -1,
      max: 1
    },
    zAxis3D: {
      min: -1,
      max: 1
    },
/*    legend: {
      tooltip: {
        // show: true,
        trigger: 'item',
/!*        axisPointer: {
          type: 'cross',
          label: {
            backgroundColor: '#6a7985'
          }
        },*!/
        formatter:e=>{
          return `
	              ${e[0].marker} 未带安全帽: ${e[0].data.value1}<br/>
	              ${e[0].marker} 未穿反光衣: ${e[0].data.value2}
	            `
        }
      },
/!*      icon: 'none',
      orient: 'vertical',
      right: '5%',
      top: '20%',
      itemGap: 30,
      itemWidth: 12,
      itemHeight: 12,
      selectedMode: false, // 关闭图例选择
      textStyle: {
        color: '#fff',
        fontSize: 14,
        fontFamily: 'Source Han Sans CN',
        rich: {
          // iconName: {
          //   width: 5,
          //   height: 8,
          //   backgroundColor: '#ff00ff'
          // },
          name: {
            color: '#FFF',
            fontSize: 14,
            // width: 140,
            padding: [0, 0, 0, 10],
          },
          value: {
            color: '#2BDFD4',
            fontFamily: 'PangMenZhengDao',
            fontSize: 14,
            // width: 80,
            padding: [0, 0, 0, 10]
          },
          percent: {
            color: '#2BDFD4',
            fontFamily: 'PangMenZhengDao',
            fontSize: 14,
            padding: [0, 0, 0, 10]
          },
          unit: {
            color: '#ACDCE4',
            fontSize: 14,
            padding: [0, 0, 0, 2]
          }
        }
      },
      formatter: name => {
        let obj = pieData.find(item => item.name === name);
        let datas = pieData;
        let total = 0;
        let target = obj.value
        for (let i = 0; i < datas.length; i++) {
          total += Number(datas[i].value);
        }
        const arr = [
          `{iconName|}{name|${name}}{value|${obj.value}个}{percent|占比 ${((target / total) * 100).toFixed(0)}}{unit|%}`
        ];
        /!*        const arr = [
                  `{iconName|}{name|${name}}{percent|${((target / total) * 100).toFixed(0)}}{unit|%}`
                ];*!/
        return arr.join('')
      }*!/
    },*/
    grid3D: {
      show: false,
      boxHeight: 30,
      height: '200%',
      left: '-5%',
      top: '-66%',
      width: '100%',
      viewControl: {
        alpha: 30,
        beta: -10, // 饼块开始得角度
        rotateSensitivity: 1,
        zoomSensitivity: 0,
        panSensitivity: 0,
        autoRotate: true,
        distance: 400,
        //   autoRotateSpeed: 5,
        //   autoRotateAfterStill: 10
      },
    },
    series: series
  };
  return obj;
}
</script>

<style scoped lang="scss">

.left-body {

  .content-bty {
    width: 100%;
    height: 90%;
    position: absolute;
    top: 7%;
    right: 7%;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: flex-start;
    font-family: fangzheng;

    .item-box {
      margin-bottom: 15px;
      padding-bottom: 10px;
      //background: url("@/assets/imgs/securityManage/rectangle.png") no-repeat center;
      //background-size: inherit;
      //background: linear-gradient(to left, rgba(93, 122, 164, 0.5), rgba(145, 177, 224, 0.16));
      width: 410px;
      position: relative;

      &::before {
        content: ' ';
        background: url("@/assets/images/box-left.png") no-repeat center;
        width: 40px;
        height: 100%;
        background-size: contain;
        position: absolute;
        z-index: 100;
        left: -30px;
        top: 0;
      }

      &::after {
        content: ' ';
        background: url("@/assets/images/box-right.png") no-repeat center;
        width: 40px;
        height: 100%;
        background-size: contain;
        position: absolute;
        z-index: 100;
        right: -30px;
        top: 0;
      }

      .item-box-name {
        width: 90%;
      }

      .item-box-pd {
        width: 100%;
        text-align: center;
        //padding: 10px 15px;
      }
    }

    .chart-sty {
      display: flex;
      align-items: center;
      justify-content: flex-start;

      .chart-sty-item {

        & > div {
          margin: 17px 0;
          display: flex;
          justify-content: flex-start;
          align-items: flex-start;
          background: rgba(255, 255, 255, 0.29);
          border-radius: 4px;
          width: 196px;

          span {
            margin-left: 5px;
          }
        }

        img {
          width: 24px;
          height: 24px;
        }
      }
    }

    .video-sty {
      width: 100%;
      height: 200px;
      object-fit: fill;
    }

    .people-content {
      display: flex;
      flex-direction: row;
      align-items: center;
      margin-top: 10px;
      position: relative;
      padding: 10px;
      background: url("@/assets/imgs/tencelgy1.png") no-repeat center;
      background-size: contain;

      &>img {
        width: 100px;
        height: 110px;
        object-fit: fill;
      }

      .people-info {
        display: inherit;
        align-items: flex-start;
        justify-content: inherit;
        flex-direction: column;
        margin-left: 5px;
      }

      .people-warn {
        position: absolute;
        right: 12px;
        top: 12px;

        img {
          width: 13px;
          height: 12px;
        }

        & > span {
          font-size: 14px;
          color: #FF8A1F;
          margin-left: 2px;
        }
      }
    }

    .history {
      width: 50%;
      height: 100px;
      padding: 10px;
    }

    .history-table {
      width: 100%;

      .history-table-name {
        display: flex;
        flex-direction: row;
        align-items: center;
        font-size: 16px;
        color: #dedede;
        background: rgba(0,64,175,0.8);
        padding: 2px 5px;

        span {
          word-wrap: break-word;
        }
        span:nth-child(1) {
          flex: 0 0 20%;
        }
        span:nth-child(2) {
          flex: 0 0 40%;
        }
        span:nth-child(3) {
          flex: 0 0 40%;
        }
      }

      .history-table-list {
        width: 100%;
        height: 90px;
        overflow-y: auto;

        li {
          display: flex;
          flex-direction: row;
          align-items: center;
          line-height: 18px;
          border-bottom: 1px solid blue;
          padding: 7px 0;
          //text-align: center;
        }

        li > a {
          word-wrap: break-word;
        }

        li > a:nth-child(1) {
          flex: 0 0 20%;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        li > a:nth-child(2) {
          flex: 0 0 40%;
        }
        li > a:nth-child(3) {
          flex: 0 0 40%;
        }
      }
    }

    .anylsis {
      display: flex;
      align-items: center;
      justify-content: center;

      .anylsis-cahrt {
        width: 100%;
        height: 260px;
        margin: 10px 0;
      }
    }
  }

}
</style>
